//
//  ZZXStrategyViewController.m
//  Travel
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "ZZXStrategyViewController.h"
#import "ZZXNiceScrollView.h"
#import "CXNetHandle.h"
#import "ZZXstrategryModel.h"
#import "ZZXLoactionDetailPageViewController.h"
#import "ZZXTableCollectionViewCell.h"

@interface ZZXStrategyViewController ()<ButtonChangeFontSize, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong)ZZXNiceScrollView *titleView;
@property (nonatomic, strong)UICollectionView *collection;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign)NSInteger key;
@property (nonatomic, strong) NSMutableArray *LatestDataArr; //最新页对象数组
@property (nonatomic, strong)NSMutableArray *popularDataArr;//最热
@property (nonatomic, strong)NSMutableArray *AsiaDataArr;//亚洲
@property (nonatomic, strong)NSMutableArray *EuropeanDataArr;//欧洲
@property (nonatomic, strong)NSMutableArray *AmericasDataArr;//美洲
@property (nonatomic, strong)NSMutableArray *AfricaDataArr;//非洲
@property (nonatomic, strong)NSMutableArray *OceaniaDataArr;//大洋洲
@property (nonatomic, copy)void(^PUSHBLOCK)(NSNumber *);


@end

@implementation ZZXStrategyViewController


- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
//    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
//    self.titleLabel.text = @"攻略";
//    self.titleLabel.textColor = [UIColor purpleColor];
//    self.titleLabel.textAlignment = NSTextAlignmentCenter;
//    self.navigationItem.titleView = self.titleLabel;
    //边缘拖曳
     [self addScreenEdgePan];
    self.title = @"攻略";
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];

//    [self loadMBProgress];
    [CXLoadMBProgress loadMBProgress:self.view];
  
    [self getNewData];
    
//BLOCK传值
    //在BLOCK函数中不能用强指针 需转化为弱指针
    
    __weak ZZXStrategyViewController *weakSelf = self;
    self.PUSHBLOCK = ^(NSNumber *itemid){
        ZZXLoactionDetailPageViewController *location = [[ZZXLoactionDetailPageViewController alloc]init];
        //隐藏下面的tabBar
        location.hidesBottomBarWhenPushed = YES;
        location.itemid = itemid;
        [weakSelf.navigationController pushViewController:location animated:YES];
       
        
    };
    
}

#pragma mark - 获取最新页数据
- (void)getNewData
{
    //7个界面的api
    NSString *LatestUrl = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=9927&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=f448fd13669fbd2e6df0ff7ac1e1356d";//最新
    NSString * PopularUrl = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=17431&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=70167f27a9e8c78482cd40b158643394";//热门
    
    
    NSString *AsiaUrl = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23213&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=13bb1c5771fc2b1a97bc31402b6afd88";//亚洲
    
    NSString *EuropeanUrl = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23423&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=1cb0345d6bee388e565af00571b3f71a"; //欧洲
    NSString *AmericasUrl = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23647&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=a3a627ca7c167a09e420889c5fcb2abf"; //美洲
    NSString *AfricaUrl = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23549&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=761bdfadd0a6c09f7552bdaf09d4ac11"; //非洲
    NSString *OceaniaUrl= @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23339&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=95a73c75f1b83fb2b66a17dde471b7ca"; //大洋洲
    NSMutableArray *urlArr = [@[LatestUrl, PopularUrl, AsiaUrl, EuropeanUrl, AfricaUrl, AmericasUrl, OceaniaUrl]mutableCopy]; //url数组
    for (NSString *url in urlArr) {
        [CXNetHandle getUrl:url cookie:nil result:^(id result) {
            NSMutableDictionary *dic = result;
            NSMutableArray *tempArr =  dic[@"obj"][@"list"];
            self.dataArr = [NSMutableArray array];
            for (NSMutableDictionary *dic in tempArr) {
                for (NSMutableDictionary *tempDic in dic[@"list"]) {
                    [self.dataArr addObject:tempDic];
                    //每一个tableView总数据
         
                }
            
}
                //依次判断 以防止每个接口请求速度不同  导致请求到数据顺序混乱
            if ([url isEqualToString:LatestUrl]) {
                self.LatestDataArr = [ZZXstrategryModel transformWithArray: self.dataArr];
                
            }
            
            else if ([url isEqualToString:PopularUrl])
            {
                self.popularDataArr = [ZZXstrategryModel transformWithArray: self.dataArr];
            }
            else if ([url isEqualToString:AsiaUrl])
            {
                self.AsiaDataArr = [ZZXstrategryModel transformWithArray: self.dataArr];
            }
            else if ([url isEqualToString:EuropeanUrl])
            {
                self.EuropeanDataArr = [ZZXstrategryModel transformWithArray:self.dataArr];
            }
            else if ([url isEqualToString:AfricaUrl])
            {
                self.AmericasDataArr = [ZZXstrategryModel transformWithArray:self.dataArr];
            }
            else if ([url isEqualToString:AmericasUrl])
            {
                self.AfricaDataArr = [ZZXstrategryModel transformWithArray:self.dataArr];
            }
            else if ([url isEqualToString:OceaniaUrl])
            {
                self.OceaniaDataArr = [ZZXstrategryModel transformWithArray:self.dataArr];
            }
            
            //记录最后一个url获取成功 在创建
            self.key++;
            if (self.key == urlArr.count) {
            
                [self createMainScrollView];
                [self createTitleScrollView];
                
                [CXLoadMBProgress removeMBProgress:self.view];
            }
        }];
    }
    
}

#pragma mark - 创建头部scrollView
- (void)createTitleScrollView
{
    self.titleView = [[ZZXNiceScrollView alloc]initWithFrame:CGRectMake(0, 0, CWIDTH, 50)];
    //签协议
    self.titleView.delegate = self;
    
    self.titleView.tag = 4000;
    [self.view addSubview:self.titleView];

    
}

#pragma mark - titleScrollView代理协议方法
- (void)ChangeFontSizeAndColor:(UIButton *)btn
{
    //点击button时  先让其他button变成默认状态
    for (NSInteger i = 0; i < 7; i++) {
        UIButton *btn = (UIButton *)[self.titleView viewWithTag:3000 + i];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        
    }
    //collectionView的偏移量
    //改变当前button的状态
    self.collection.contentOffset = CGPointMake(CWIDTH * (btn.tag - 3000), 0);
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    
    
}

#pragma mark - 滚啊滚
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.collection) {
        //通过偏移量记录button的tag值
        NSInteger i = self.collection.contentOffset.x / CWIDTH;
        
        for (NSInteger i = 0; i < 7; i++) {
            UIButton *btn = (UIButton *)[self.titleView viewWithTag:3000 + i];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:15];
            
        }
        if (i == 5) {
            self.titleView.scrollView.contentOffset = CGPointMake(170, 0);
        }
        if (i == 1) {
            self.titleView.scrollView.contentOffset = CGPointMake(0, 0);
        }
        
        ((UIButton *)[self.titleView viewWithTag:3000 + i]).titleLabel.font = [UIFont systemFontOfSize:17];
        [((UIButton *)[self.titleView viewWithTag:3000 + i]) setTitleColor:[UIColor colorWithRed:0.71 green:0.78 blue:0.73 alpha:1] forState:UIControlStateNormal];
        
        
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
            
            [self.titleView viewWithTag:3200].frame = CGRectMake(CWIDTH / 5 * i + 25, 45, 40, 2);
            
            
        } completion:^(BOOL finished) {
            
        }];
        
    }
}
#pragma mark - 创建collectionView
- (void)createMainScrollView
{
    //创建一个瀑布流
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(CWIDTH, CHEIGHT + 50);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //section距离collection的边距
    layout.sectionInset= UIEdgeInsetsMake(0, 0, 0, 0);
    
    //item边距
    
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    //创建collectionView
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, CWIDTH , CHEIGHT - 50 ) collectionViewLayout:layout];
    
    self.collection.showsHorizontalScrollIndicator = NO;
    //整页滑动
    self.collection.pagingEnabled = YES;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    self.collection.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:247 / 255.0 blue:237 / 255.0 alpha:1];
    [self.view addSubview:self.collection];
    
    //注册cell
    [self.collection registerClass:[ZZXTableCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
//    [[self class]BoolIsNight:self.collection];
    

}


#pragma mark - collection item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return 7;
}

#pragma mark - collection cell赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   ZZXTableCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];

    if (indexPath.item == 0) {
        cell.dataArr = self.LatestDataArr;
        
        cell.webViewArr = self.LatestDataArr;
    }
    else if (indexPath.item == 1)
    {
        cell.dataArr = self.popularDataArr;
    }
    else if (indexPath.item == 2)
    {
        cell.dataArr = self.AsiaDataArr;
    }
    else if (indexPath.item == 3)
    {
        cell.dataArr = self.EuropeanDataArr;
    }
    else if (indexPath.item == 4)
    {
        cell.dataArr = self.AmericasDataArr;
    }
    else if (indexPath.item == 5)
    {
        cell.dataArr = self.AfricaDataArr;
    }
    else if (indexPath.item == 6)
    {
        cell.dataArr = self.OceaniaDataArr;
    }
    
//     cell.dataArr = self.modelAsialDataArr[indexPath.item - 1];
    
    cell.item = indexPath.item;
    cell.PUSHBLOCK = self.PUSHBLOCK;
    return cell;
}

#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    [self.collection addGestureRecognizer:screenEdgePan];
    
}
#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
